﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SharedLib.XmlDataModel;
using System.Xml;
using SharedLib;
using SharedLib.Helpers;

namespace Tracy.BasicServices
{
    public class UserPreferencesManager //TODO: to be replaced by TracyConfigurationManager
    {
        UserPreferences _pref;

        public UserPreferences CurrentUserPreferences
        {
            get { return _pref; }
        }
        private string FileName
        {
            get { return AppDataHelper.GetAppDataFile("UserPreferences.xml"); }
        }

        public event EventHandler<GenericEventArgs<UserPreferences>> UserPreferenceUpdated;
        public UserPreferencesManager()
        {
            XmlNode node = null;
            if (File.Exists(FileName))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FileName);
                node = doc.SelectSingleNode("/user-preference");
            }

            if (node == null)
            {
                _pref = new UserPreferences();
                _pref.FirstUseFlag = true;
            }
            else
            {
                _pref = new UserPreferences(node);
            }

            if (String.IsNullOrEmpty(_pref.SavePath)) _pref.SavePath = Tracy.Properties.Settings.Default.DefaultDownloadFolder;
            if (String.IsNullOrEmpty(_pref.TempPath)) _pref.TempPath = Tracy.Properties.Settings.Default.DefaultDownloadFolder;
        }

        public string SavePath
        {
            get
            {
                return CurrentUserPreferences.SavePath;
            }
        }

        public string TempPath
        {
            get
            {
                return CurrentUserPreferences.TempPath;
            }
        }

        public void Update(UserPreferences pref)
        {
            if (pref != null)
            {
                _pref = pref;
                UserPreferenceUpdated.InvokeEvent(this, new GenericEventArgs<UserPreferences>(pref));
                pref.ToXmlDocument().Save(FileName);
            }
        }

        public class UserPreferences : XmlDataModelWithAttributes
        {
            public UserPreferences(XmlNode node) : base(node) { }
            public UserPreferences() : base() { }

            public override string RootNodeName
            {
                get
                {
                    return "user-preference";
                }
            }

            /// <summary>
            /// Default Save Path for new Entry
            /// </summary>
            public string SavePath
            {
                get
                {
                    string rtn = Properties["save-path"];
                    if (rtn == null) return null;
                    if (!rtn.EndsWith("\\")) rtn += "\\";
                    return rtn;
                }
                set { Properties["save-path"] = value; }
            }

            /// <summary>
            /// Default Temp Path for Downloaders
            /// </summary>
            public string TempPath
            {
                get
                {
                    string rtn = Properties["temp-path"];
                    if (rtn == null) return null;
                    if (!rtn.EndsWith("\\")) rtn += "\\";
                    return rtn;
                }
                set { Properties["temp-path"] = value; }
            }

            public bool FirstUseFlag
            {
                get { return !String.Equals(Properties["first-use"], "false", StringComparison.OrdinalIgnoreCase); }
                set { Properties["first-use"] = value.ToString(); }
            }
        }


    }
}
